﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;
using SyndicateBusiness.Classes;

namespace BigLotterySyndicate.MyAccount
{
    public partial class SetUp : BasePublicPage
    {
        protected string Password
        {
            get { return this.txtPassword.Text.Trim(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {          

        }

        protected string PrintUserName()
        {
            return LoggedInUser.username;        
        }

        protected void ButtonContinue_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;

            user u = this.BusinessFacade.UserBL.GetSingleUser(LoggedInUser.id);

            u.password = Utilities.EncodePassword(this.Password);
            u.isActive = true;

            if (this.BusinessFacade.UserBL.UpdateUser())
            {
                fundsLog logentry = this.BusinessFacade.FundsLogBL.SelectAllByToUserId(u.id).SingleOrDefault(l => l.amount == -1);

                if (logentry != null)
                    this.BusinessFacade.FundsLogBL.DeleteEntry(logentry);

                if (Page.Request.QueryString["back"] != null)
                {
                    this.Redirect(Server.UrlDecode(Page.Request.QueryString["back"].ToString()));
                }
                else
                {
                    this.Redirect("/MyAccount/");
                }
            }

        }
    }
}